<?php
namespace App\Http\Middleware;
use Closure;
class OperationLog{
  /**
   * Handle an incoming request.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @return mixed
   */
  public function handle($request, Closure $next){
    $input = $request->all(); //操作的内容
    $path = $request->path();  //操作的路由
    $method = $request->method();  //操作的方法
    $ip = $request->ip();  //操作的IP
    $admin_id = 0;
    $data['input'] = json_encode($input);
    $data['path'] = $path;
    $data['method'] = $method;
    $data['ip'] = $ip;
    if($request->session()->has('user')){
      $userinfo = $request->session()->get('user');
      $admin_id = $userinfo[0]['admin_id'];
    }
    $data['admin_id'] = $admin_id;
    $nowTime = time();
    $data['create_time'] = $nowTime;
    $oAdminAction = new \App\Models\AdminModels\AdminAction();
    $aaData = $oAdminAction->db_get(['admin_id'=>['=',$admin_id],'path'=>['=',$path],'create_time'=>['=',$nowTime]]);
    if(empty($aaData)){
      $oAdminAction->db_insert($data);
    }
    return $next($request);
  }
}
